// pages/settled/settled.js
//获取应用实例
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading:true,
    id: 2,
    cartnum: 1,
    city1: '',//接货地址
    city2: '',//送货地址
    model: ['美国', '中国', '巴西', '日本'],//车型
    length: ['1', '2', '3', '4'],//车长
    volume: [1, 2, 3, 4, 5],//体积
    load: [1, 2, 3, 4, 5],//载重
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      options: options
    })
    that.getData();
  },
  getData:function(res){
    var that = this;
    app.getLoginSession(function (session) {
      //获取司机信息
      wx.request({
        url: app.api + '/wx_log_test/sys/info',
        data: {
          token: session,
          driverid: that.data.options.driverid
        },
        success: function (res) {
          console.log(res);
          var res = res.data;
          if (res.msg == "OK") {
            var data = res.data;
            that.setData({
              driver: data.driver,
              list: data.list,
              loading: false
            })
            console.log(that.data.content);
          } else {
            that.setData({
              loading: false,
              loadtips: res.msg
            })
          }
        },
        fail: function (res) {
          that.setData({
            loading: true
          })
        }
      })
      //获取分组
      wx.request({
        url: app.api + '/wx_log_test/sys/group',
        data: {
          token: session
        },
        success: function (res) {
          var res = res.data;
          if (res.msg == "OK") {
            var data = res.data;
            that.setData({
              group: that.deallist(data)
            })
          }
        }
      })
    })
  },
  actcart: function () {
    var that = this;
    that.setData({
      cartnum: that.data.cartnum + 1
    })
  },
  switch: function (e) {
    console.log(e);
    wx.navigateTo({
      url: '/pages/switchcity/switchcity?id=' + e.currentTarget.dataset.id,
    })
  },
  //车型
  modelChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      in_model: e.detail.value
    })
  },
  //车长
  lengthChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      in_length: e.detail.value
    })
  },
  //体积
  volumeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      in_volume: e.detail.value
    })
  },
  //载重
  loadChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      in_load: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (app.globalData.city1 != '') {
      that.setData({
        city1: app.globalData.city1
      })
    }
    if (app.globalData.city2 != '') {
      that.setData({
        city2: app.globalData.city2
      })
    }
  },
  setModalStatus: function (e) {
    console.log("设置显示状态，1显示0不显示", e.currentTarget.dataset.status, e.currentTarget.dataset.active);
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    var that = this;
    that.setData({
      active: e.currentTarget.dataset.active
    })
    console.log(that.data.active);
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export()
    })
    if (e.currentTarget.dataset.status == 1) {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation
      })
      if (e.currentTarget.dataset.status == 0) {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200)
  },
  // 拒绝原因
  inputreason:function(res){
    var that = this;
    that.setData({
      reason:res.detail.value
    })
  },
  //输入完成
  reasonsubmit:function(e){
    var that = this;
    that.setData({
      active: 'refuse'
    })
    if(that.data.reason==null||that.data.reason==""){
      wx.showToast({
        title: '请输入原因',
        icon:'loading'
      })
    }else{

      console.log("设置显示状态，1显示0不显示", e.currentTarget.dataset.status);
      var animation = wx.createAnimation({
        duration: 200,
        timingFunction: "linear",
        delay: 0
      })
      that.animation = animation
      animation.translateY(300).step()
      that.setData({
        animationData: animation.export()
      })
      if (e.currentTarget.dataset.status == 1) {
        that.setData(
          {
            showModalStatus: true
          }
        );
      }
      setTimeout(function () {
        animation.translateY(0).step()
        that.setData({
          animationData: animation
        })
        if (e.currentTarget.dataset.status == 0) {
          that.setData(
            {
              showModalStatus: false
            }
          );
        }
      }.bind(that), 200)
      that.submit();
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  radioChange: function (e) {
    var that = this;
    console.log(e);
    that.setData({
      groupid: e.detail.value
    })
  },
  deallist(list) {
    if (list && Array.isArray(list)) {
      list.forEach((item) => {
        item.checked==false
      })
      return list
    }
  },
  //提交审核信息
  submit:function(){
    var that = this;
    console.log(that.data.active);
    if (that.data.active=="pass"){//审核通过
      if(that.data.groupid==undefined){
        wx.showModal({
          title: '提示',
          content: '请选择分组'
        })
        return false;
      }else{
        that.data.driver.status=1,
          that.data.driver.groupId = that.data.groupid
        that.setData({
          driver:that.data.driver
        })
        app.getLoginSession(function (session) {
          wx.request({
            url: app.api + '/wx_log_test/driver/approve?token='+session,
            method: 'POST',
            header: {
              'content-type': 'application/json' // 默认值
            },
            data: {
              status:1,
              groupId: that.data.groupid,
              approvedInfo:'',
              driverId:that.data.options.driverid
            },
            success: function (res) {
              var res = res.data;
              if(res.msg=="OK"){
                wx.navigateBack({
                  delta:1
                })
              }else{
                wx.showToast({
                  title: '请重试！',
                  icon:'loading',
                  duration: 1000
                })
              }
            },
            fail:function(res){
              wx.showModal({
                title: '提示',
                content: '接口请求失败'
              })
            }
          })
        })
        
      }
    }else{
      if (that.data.reason == null || that.data.reason == "") {
        wx.showToast({
          title: '请输入原因',
          icon: 'loading'
        })
      } else {
        that.data.driver.status = -1,
          that.data.driver.approvedInfo = that.data.reason
        that.setData({
          driver: that.data.driver
        })
        app.getLoginSession(function (session) {
          wx.request({
            url: app.api + '/wx_log_test/driver/approve?token=' + session,
            method: 'POST',
            header: {
              'content-type': 'application/json' // 默认值
            },
            data: {
              status: -1,
              groupId: '',
              approvedInfo: that.data.reason,
              driverId: that.data.options.driverid
            },
            success: function (res) {
              var res = res.data;
              if (res.msg == "OK") {
                wx.navigateBack({
                  delta: 1
                })
              } else {
                wx.showToast({
                  title: '请重试！',
                  icon: 'loading',
                  duration: 1000
                })
              }
            },
            fail: function (res) {
              wx.showModal({
                title: '提示',
                content: '接口请求失败'
              })
            }
          })
        })
      }
     
    }
  },
  // 预览图片
  showimage:function(res){
    var that = this;
    var image = res.currentTarget.dataset.img;
    wx.previewImage({
      current:image,
      urls:[image]
    })
  }
})